//
//  ShellFriendView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/9/26.
//  Copyright © 2019年 VV. All rights reserved.
//

#import "ShellFriendView.h"
#import "ShellFriendCell.h"
#import "LZBaseTableView.h"
#import "ShellFriendViewModel.h"

@interface ShellFriendView ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) ShellFriendViewModel *viewModel;

//滑动的View
@property (nonatomic,strong) LZBaseTableView *tableView;
@end


@implementation ShellFriendView

- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        
        self.viewModel = (ShellFriendViewModel *) viewModel;
        
        [self setupView];
    }
    
    return self;
}


#pragma mark - setupView
- (void)setupView{
    
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);

    }];
    
    UIView *headView = [[UIView alloc] init];
    headView.frame = CGRectMake(0, 0, K_SCREENWIDTH, 10);
    self.tableView.tableHeaderView = headView;
    
    
    @weakify(self);
    [self.tableView addHeadWithRefreshingBlock:^{
        @strongify(self);
        [self.viewModel getMyFriend:YES];
    }];
    
    [self.tableView addAutoNormalFooterWithRefreshingBlock:^{
        @strongify(self);
        [self.viewModel getMyFriend:NO];
    }];
    
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self reloadRecordData:[x integerValue]];
    }];
}

- (void)reloadRecordData:(NSInteger)type{
    self.tableView.mj_header.state  = MJRefreshStateIdle;
    self.tableView.mj_footer.state  = MJRefreshStateIdle;
    [self.tableView reloadData];
    [self.tableView dismissEmptyView];

    switch (type) {
        case 0:{
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }
            break;
        default:
            break;
    }

    if(self.viewModel.dataArray.count == 0){
        self.tableView.mj_footer.hidden = YES;
        [self.tableView tableViewDisplayWithImgName:@"pic_nodata" message:LZLocalizedString(@"暂无数据") contentoffsetY:109 showEmptyView:YES];
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShellFriendCell *friendCell = [tableView dequeueReusableCellWithIdentifier:@"ShellFriendCell"];
    friendCell.modelObject = self.viewModel.dataArray[indexPath.row];
    return friendCell;
}


#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
  
}

#pragma mark - get && set
- (LZBaseTableView *)tableView{
    if(!_tableView){
        _tableView = [[LZBaseTableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = COLOR_BACKGROUND;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = 100;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerClass:[ShellFriendCell class] forCellReuseIdentifier:@"ShellFriendCell"];
    }
    return _tableView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
